﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Inference
{
	public partial class DomainBuilder : Form
	{
		TaleEntities tale;
		frmMain parentForm;
		List<string> alreadyCutTexts;
		string firstChosenDomain;
		public DomainBuilder()
		{
			InitializeComponent();
		}

		public string  Except
		{
			get;
			set;
		}

		public string  ChosenDomainName
		{
			get
			{
				if (cmbBaseDomain.SelectedIndex != -1)
				{
					return cmbBaseDomain.SelectedItem.ToString();
				}
				return null;
			}
		}

		public DomainBuilder(frmMain parentForm, string chosenDomain, List<TreeNode> alreadyCut, string except)
		{
			InitializeComponent();

			tale = new TaleEntities();
			this.alreadyCutTexts = new List<string>();
			this.parentForm = parentForm;
			foreach (TreeNode item in alreadyCut)
			{
				this.alreadyCutTexts.Add(item.Text);
			}
			firstChosenDomain = chosenDomain;
			Except = except;
			LoadCmb(chosenDomain, except);
		}

		public void LoadCmb(string chosenDomain, string except)
		{
			cmbBaseDomain.Items.Clear();

			var doms = tale.Domains.Where(d => d.ID != 1 && d.ID != 7).OrderBy(d => d.Name).ToList();
			foreach (var item in doms)
			{
				if (item.Name != except)
				{
					if (except == null || (except != null && DomainHierarchier.IsEmbracing(item.Name, except, tale)))
					{
						int q = cmbBaseDomain.Items.Add(item.Name);
						if (item.Name == chosenDomain)
						{
							cmbBaseDomain.SelectedIndex = q;
						}
					}
				}
			}
			if (cmbBaseDomain.Items.Count == 0)
			{
				cmbBaseDomain.Enabled = false;
			}
			else if (cmbBaseDomain.SelectedIndex == -1)
			{
				cmbBaseDomain.SelectedIndex = 0;
			}

		}

		private void cmbBaseDomain_SelectedIndexChanged(object sender, EventArgs e)
		{
			tvDomain.Nodes.Clear();

			if (cmbBaseDomain.SelectedIndex != -1)
			{
				string s = cmbBaseDomain.SelectedItem.ToString();
				var doms = tale.Domains.Where(d => d.Name == s).ToList();
				if (doms.Count > 0)
				{
					tvDomain.Enabled = true;
					var dom = doms[0];
					tvDomain.Nodes.Add(parentForm.CreateDomainNode(dom));
					if (cmbBaseDomain.SelectedItem.ToString() == firstChosenDomain)
					{
						foreach (TreeNode item in tvDomain.Nodes[0].Nodes)
						{
							item.Checked = alreadyCutTexts.Contains(item.Text);
						}
					}

					tvDomain.ExpandAll();
				}
				else
				{
					cmbBaseDomain.SelectedIndex = 0;
				}
			}
		}

		bool blockRoot = false;
		bool blockChilds = false;

		private void tvDomain_AfterCheck(object sender, TreeViewEventArgs e)
		{
			TreeNode t = e.Node;
			if (t.Level == 0)
			{
				if (!blockRoot)
				{
					blockChilds = true;
					foreach (TreeNode item in t.Nodes)
					{
						item.Checked = t.Checked;
					}
					blockChilds = false;
				}
			}
			else
			{
				if (!blockChilds)
				{
					bool anyChecked = false;
					foreach (TreeNode item in t.Parent.Nodes)
					{
						if (item.Checked)
						{
							anyChecked = true;
							break;
						}
					}
					blockRoot = true;
					t.Parent.Checked = anyChecked;
					blockRoot = false;
				}
			}
		}

		private void tvDomain_BeforeCheck(object sender, TreeViewCancelEventArgs e)
		{

		}

		public string BaseDomainName
		{
			get
			{
				if (cmbBaseDomain.SelectedIndex != 0)
					return cmbBaseDomain.SelectedItem.ToString();
				else
					return null;
			}
		}

		public List<string> ChosenValuesNames
		{
			get
			{
				List<string> res = new List<string>();
				if (cmbBaseDomain.SelectedIndex != -1)
					foreach (TreeNode item in tvDomain.Nodes[0].Nodes)
					{
						if (item.Checked)
						{
							res.Add(item.Text);
						}
					}
				return res;
			}
		}

		private void DomainBuilder_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (this.DialogResult == System.Windows.Forms.DialogResult.OK)
			{
				bool allChecked = true;
				bool anyChecked = false;
				foreach (TreeNode item in tvDomain.Nodes[0].Nodes)
				{
					if (item.Checked)
					{
						anyChecked = true;
					}
					else
					{
						allChecked = false;
					}
				}
				if (allChecked)
				{
					//	parentForm.InformUser("Поддомен должен быть меньше базового домена по крайней мере на одно значение.");
					//	e.Cancel = true;
				}
				if (!anyChecked)
				{
					parentForm.InformUser("Поддомен должен состоять крайней мере из одного значения.");
					e.Cancel = true;
				}


			}
		}
	}
}
